package com.apartmentManage.controller;


import com.apartmentManage.entity.AbsenceLog;
import com.apartmentManage.service.AbsenceLogService;
import com.apartmentManage.util.Renter;
import com.apartmentManage.util.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("/absenceLog")
public class AbsenceLogController {

    @Autowired
    private AbsenceLogService absenceLogService;

    @GetMapping("/addView")
    String addView(){
        return "pages/absenceLog/add";
    }


    /*添加*/
    @PostMapping("/save")
    String save(AbsenceLog absenceLog){
        boolean flag = absenceLogService.save(absenceLog);
        new Renter(flag);
        if (flag)
            return "redirect:/absenceLog/getAll";
        return  "redirect:/absenceLog/addView";
    }

    @GetMapping("/updateView/{sno}")
    String updateView(@PathVariable("sno") String sno,Model model){
        AbsenceLog result = absenceLogService.getByNo(sno);
        model.addAttribute("result",result);
        return "pages/absenceLog/update";
    }

    /*修改*/
    @PostMapping("/update")
    String update( AbsenceLog absenceLog){
        boolean flag = absenceLogService.update(absenceLog);
        new Renter(flag);
        if (flag)
            return "redirect:/absenceLog/getAll";
        return "redirect:/absenceLog/updateView/"+absenceLog.getSno();
    }

    /*删除*/
    @GetMapping("/delete/{sno}")
    String delete(@PathVariable("sno") String sno){
        boolean flag = absenceLogService.delete(sno);
        new Renter(flag);
        return "redirect:/absenceLog/getAll";
    }

    /*查询所有*/
    @GetMapping("/getAll")
    String getAll(Model model){
        List<AbsenceLog> result = absenceLogService.getAll();
        new Renter(result==null?false:true,result);
        model.addAttribute("result",result);
        return "pages/absenceLog/List";
    }

    /*条件模糊查询*/
    @GetMapping("/getByRequirement")
   String getByRequirement(AbsenceLog absenceLog,Model model){
        List<AbsenceLog> result = absenceLogService.getByRequirement(absenceLog);
        new Renter(result==null?false:true,result);
        model.addAttribute("result",result);
        return "pages/absenceLog/List";
   }

}
